<template>
	<om-addpage :menuName="menuName" :busCode="busCode" @formSubmit="formSubmit" :scrollTop="scrollTop">
		<view v-if="!backLeaveTimeList.length">
			<om-white-space size="large"></om-white-space>
			<view style="font-size: 14px;color: #A7A7A7;" class="tui-center">您没有需要销假的请假单</view>
			<om-white-space size="large"></om-white-space>
		</view>
		<view v-else v-for="(item, index) in backLeaveTimeList" :key="index" style="display: block;">
			<uni-list :title="'销假信息' + (index || '')">
				<uni-list-item :showArrow="false">
					<view class="tui-flex">
						<view class="tui-col-4"><om-label label="姓名"></om-label></view>
						<view class="tui-center  tui-col-8">
							<view class="tui-right">{{ item.infoName }}</view>
						</view>
					</view>
				</uni-list-item>
				<uni-list-item  :showArrow="false">
					<view class="tui-flex">
						<view class="tui-col-4"><om-label label="请假类别"></om-label></view>
						<view class="tui-center  tui-col-8">
							<view class="tui-right">{{ item.leaveClassName }}</view>
						</view>
					</view>
				</uni-list-item>
				<uni-list-item  :showArrow="false">
					<view class="tui-flex">
						<view class="tui-col-4"><om-label label="请假开始时间"></om-label></view>
						<view class="tui-center  tui-col-8">
							<view class="tui-right">{{ item.startTime }}</view>
						</view>
					</view>
				</uni-list-item>
				<uni-list-item  :showArrow="false">
					<view class="tui-flex">
						<view class="tui-col-4"><om-label label="请假结束时间"></om-label></view>
						<view class="tui-center  tui-col-8">
							<view class="tui-right">{{ item.endTime }}</view>
						</view>
					</view>
				</uni-list-item>
				<uni-list-item >
					<view class="tui-flex">
						<view class="tui-col-4"><om-label label="实际结束时间" required></om-label></view>
						<view class="tui-center  tui-col-8">
							<picker
								mode="multiSelector"
								:value="item.dateTime"
								:data-index="index"
								@change="changeTime"
								@columnchange="changeTimeColumn"
								:range="item.dateTimeArray"
							>
								<view class="tui-right">{{ item.dateTimeName }}</view>
							</picker>
						</view>
					</view>
				</uni-list-item>
			</uni-list>
		</view>
	</om-addpage>
</template>

<script>
import dateTimePicker from '@/static/js/dateTimePicker.js';
import { ignoreHtml, confirmModal, errorModal } from '@/static/js/common.js';
import { mapState } from 'pinia';
import {useLoginUserStore} from '@/stores/login-user.js';
export default {
	provide() {
		return {
			formPage: this
		};
	},
	data() {
		return {
			formdata: {
				userid:'',
				idStr: '',
				relateBusId: '',
				relateFormCode: '',
				relateNumberStr: ''
			},
			backLeaveTimeList: [],
			leaveType: '',
			startTime: '',
			endTime: '',
			endTimeArray: [],
			endTimeName: '',
			endTime1: '',
			menuName: '',
			id: '',
			busCode: '',
			scrollTop: 0
		};
	},
	computed: mapState(useLoginUserStore, ['userId']),
	methods: {
		/**
		 * 实际结束时间面板滑动方法
		 */
		changeTimeColumn(e) {
			if (e.detail.column == 1) {
				let backLeaveTimeList = this.backLeaveTimeList;
				let index = e.target.dataset.index;
				let backLeaveTime = backLeaveTimeList[index];

				let arr = backLeaveTime.dateTime;
				let dateArr = backLeaveTime.dateTimeArray;
				arr[e.detail.column] = e.detail.value;
				dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
				this.backLeaveTimeList = backLeaveTimeList;
			}
		},

		/**
		 * 获取实际结束时间
		 */
		changeTime(e) {
			let backLeaveTimeList = this.backLeaveTimeList;
			let index = e.target.dataset.index;
			let backLeaveTime = backLeaveTimeList[index];
			let cdate = dateTimePicker.formatterDate(backLeaveTime.dateTimeArray, e.detail.value, 4);
			//开始时间 大于 实际结束时间
			if (dateTimePicker.compareTo(backLeaveTime.startTime, cdate) > 0) {
				this.om.toast('实际结束时间要大于请假开始时间', 3000);
			} else if (dateTimePicker.compareTo(cdate, backLeaveTime.endTime) > 0) {
				//实际结束时间 大于 结束时间
				this.om.toast('实际结束时间不能大于请假结束时间', 3000);
			} else {
				backLeaveTime.dateTime = e.detail.value;
				backLeaveTime.dateTimeName = cdate;
				// this.setData({
				//   backLeaveTimeList: backLeaveTimeList,
				// });
				this.getLeaveTime(backLeaveTime.startTime, backLeaveTime.dateTimeName, backLeaveTime, backLeaveTimeList);
			}
		},
		/**
		 * 获取请假时长
		 */
		getLeaveTime(start, end, backLeaveTime, backLeaveTimeList) {
			let _that = this;
			_that.om
				.request('wxBackLeave/getLeaveTime', {
					startTime: start,
					endTime: end
				})
				.then(data => {
					if (data.succeed) {
						let obj = data.value;
						let day = '';
						let hours = '';
						let min = '';
						if (obj) {
							if (obj.day || obj.day == 0) {
								day = obj.day;
							}
							if (obj.hours || obj.hours == 0) {
								hours = obj.hours;
							}
							if (obj.min || obj.min == 0) {
								min = obj.min;
							}
						}
						backLeaveTime.day = day;
						backLeaveTime.hours = hours;
						backLeaveTime.min = min;
						_that.backLeaveTimeList = backLeaveTimeList;
					} else {
						_that.om.toast(ignoreHtml(data.msg), 3000);
					}
				});
		},
		formSubmit(formdata) {
			let _that = this;
			if(!formdata){
				formdata = {}
			}
			let tip = '';
			if (!_that.formdata.relateBusId || !_that.formdata.relateFormCode || !_that.formdata.relateNumberStr) {
				tip = '没有未销假的请假信息';
			} else if (_that.backLeaveTimeList) {
				let backLeaveTimeList = _that.backLeaveTimeList;
				for (let i = 0; i < backLeaveTimeList.length; i++) {
					let backLeaveTime = backLeaveTimeList[i];
					if (!backLeaveTime.dateTimeName) {
						tip = '实际结束时间不能为空';
					} else if (!backLeaveTime.day && backLeaveTime.day != 0) {
						tip = '请假天数不能为空';
					} else if (!backLeaveTime.hours && backLeaveTime.hours != 0) {
						tip = '请假小时不能为空';
					} else if (!backLeaveTime.min && backLeaveTime.min != 0) {
						tip = '请假分钟不能为空';
					}
					if (tip) {
						break;
					}
					//调整赋值以对应 销假单对象属性
					backLeaveTime.startTime = backLeaveTime.startTime + ':00';
					backLeaveTime.endTime1 = backLeaveTime.endTime + ':00';
					backLeaveTime.endTime = backLeaveTime.dateTimeName + ':00';
					backLeaveTime.leaveDay = backLeaveTime.day;
					backLeaveTime.leaveHours = backLeaveTime.hours;
					backLeaveTime.leaveMin = backLeaveTime.min;
				}
			}
			if (tip) {
				_that.om.toast(tip);
			} else {
				formdata = Object.assign({}, formdata, _that.formdata);
				formdata.backLeaveTimeStr = JSON.stringify(_that.backLeaveTimeList);
				_that.om.formSave('wxBackLeave/doAdd', formdata, _that);
			}
		}
	},
	onLoad(options) {
		let _that = this;
		_that.menuName = options.title || '销假';
		uni.setNavigationBarTitle({
			title: _that.menuName
		});
		_that.busCode = options.code;
		_that.om.request('wxBackLeave/getConst').then(res => {
			_that.busCode = res.value.busCode || 'HRS1202';
		});
		// 获取完整的年月日 时分秒，以及默认显示的数组
		let obj = dateTimePicker.dateTimePicker();
		// 精确到分的处理，将数组的秒去掉
		obj.dateTimeArray.pop();
		if (!_that.userId) {
			_that.om.toast('登录人员ID不存在');
			return;
		}
		_that.om
			.request(
				'wxBackLeave/queryData1',
				{
					userId: _that.userId
				},
				'post'
			)
			.then(res => {
				let rows1 = res.rows;
				if (rows1.length > 0) {
					let relateBusId = rows1[0].busId;
					let relateFormCode = rows1[0].ztcode;
					let idStr = '';
					let relateNumber = [];
					//拼接关联主表id
					for (let i = 0; i < rows1.length; i++) {
						if (i == 0) {
							idStr += rows1[i].dvid;
						} else {
							idStr += '、' + rows1[i].dvid;
						}
						//表单关联回写数据
						relateNumber.push({
							relateBus: {
								id: rows1[i].busId
							},
							relateFormCode: rows1[i].ztcode,
							idField: rows1[i].dvid,
							totalNum: 1,
							usedNum: 0,
							leftNum: 1
						});
					}
					_that.om
						.request('wxBackLeave/getChildData', {
							id: idStr
						})
						.then(res2 => {
							if (res2.succeed) {
								let backLeaveTimeList = res2.value.taskLeaves;
								for (let i = 0; i < backLeaveTimeList.length; i++) {
									//实际结束时间默认赋值请假结束时间
									let dateTime = dateTimePicker.getDateArry(obj.dateTimeArray, backLeaveTimeList[i].endTime);
									backLeaveTimeList[i].dateTime = dateTime;
									backLeaveTimeList[i].dateTimeArray = obj.dateTimeArray;
									backLeaveTimeList[i].dateTimeName = dateTimePicker.formatterDate(obj.dateTimeArray, dateTime, 4);
								}
								_that.formdata.idStr = idStr;
								_that.formdata.userid = _that.userId;
								_that.formdata.relateBusId = relateBusId;
								_that.formdata.relateFormCode = relateFormCode;
								_that.formdata.relateNumberStr = JSON.stringify(relateNumber);
								_that.backLeaveTimeList = backLeaveTimeList;
							} else {
								_that.om.toast('请假明细数据，获取失败');
							}
						});
				} else {
					_that.om.toast('没有未销假的请假信息');
				}
			});
	},
	onPageScroll(e) {
		this.scrollTop = e.scrollTop;
	}
};
</script>

<style></style>
